package psnl.bingo.mr.demo4;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class D4Reducer extends Reducer<D4Line, NullWritable, D4Line, NullWritable> {
    @Override
    protected void reduce(D4Line key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        // values对应Mapper输出的k-v键值对儿中，某一个分组的v的数据（values对应的key是相同的）

        String city=null, obj=null, month=null;
        int dataSum = 0;

        for (NullWritable v : values) {
            if(city == null) { // 得到key对象中，相同属性的信息
                city = key.getCity();
                obj = key.getObj();
                month = key.getMonth();
            }

            dataSum += key.getData();;
        }

        context.write(new D4Line(city, obj, month, dataSum), NullWritable.get());
    }
}
